package com.jinSgr.md5_sm2_encryption;

import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import kotlin.jvm.Synchronized;


public class CommandJavaUtils {


    @RequiresApi(api = Build.VERSION_CODES.O)
    public static byte[] dateTimetoHex(Calendar calendar){
        byte year = (byte) (calendar.get(Calendar.YEAR)-2000);
        byte month = (byte)(calendar.get(Calendar.MONTH)+1);
        byte day =  (byte)calendar.get(Calendar.DAY_OF_MONTH);
        byte hour =  (byte)calendar.get(Calendar.HOUR_OF_DAY);
        byte minute =  (byte)calendar.get(Calendar.MINUTE);
        byte second =  (byte)calendar.get(Calendar.SECOND);
        byte[] calendarbyte={year,month,day,hour,minute,second};

        String calendarText =conver2HexStr(calendarbyte);
//        Log.d("日期二进制",calendarText);
        String[]  calendarArr= calendarText.split(",");
        //补零
        calendarArr[0]=fillInZore(calendarArr[0],6);//year
        calendarArr[1]=fillInZore(calendarArr[1],4);//month
        calendarArr[2]=fillInZore(calendarArr[2],5);//day
        calendarArr[3]=fillInZore(calendarArr[3],5);//hour
        calendarArr[4]=fillInZore(calendarArr[4],6);//minute
        calendarArr[5]=fillInZore(calendarArr[5],6);//second
//        Log.d("日期二进制补零", Arrays.toString(calendarArr));

        List<String> c = Arrays.asList(calendarArr);
        String fillCalendar =String.join(",",c);
//        Log.d("日期二进制补零", fillCalendar);
        String fillCalendarString =fillCalendar.replace(",","");

//        Log.d("日期二进制补零", fillCalendarString);
        //协议中的反转二进制
        String calendarOK= String.join(",",stringToStringArray(fillCalendarString,8));

//        Log.d("协议中的反转二进制", calendarOK);

        String[] calendarbinOK = calendarOK.split(",");
        List<String> calendarbinOKList = Arrays.asList(calendarbinOK);
        Collections.reverse(calendarbinOKList);
        String calendarTrue =String.join(",",calendarbinOKList);

        byte[] calendarTrueByte = conver2HexToByte(calendarTrue);

//        Log.d("日期16进制补零", conver16HexStr(calendarTrueByte));
        return calendarTrueByte;
    }
    public static String fillInZore(String text,int count){
        if(text.length()<count){
            int fillNum = count-text.length();
            StringBuffer buffer = new StringBuffer();
            for (int i=0;i<fillNum;i++){
            buffer.append("0");

            }
            buffer.append(text);
            String result =buffer.toString();

            return result;
        }else {

        return text;
        }
    }



    public static String[] stringToStringArray(String src, int length) {
        //检查参数是否合法
        if (null == src || src.equals("")) {
            return null;
        }

        if (length <= 0) {
            return null;
        }
        int n = (src.length() + length - 1) / length; //获取整个字符串可以被切割成字符子串的个数
        String[] split = new String[n];
        for (int i = 0; i < n; i++) {
            if (i < (n - 1)) {
                split[i] = src.substring(i * length, (i + 1) * length);
            } else {
                split[i] = src.substring(i * length);
            }
        }
        return split;
    }

    /**
     * to 10111001
     */
    public static String  toBinString( byte tByte){

        String tString = Integer.toBinaryString((tByte & 0xFF) + 0x100).substring(1);
        Log.d("字节转二进制字符串",tString);
        return tString;
    }




    /**
     * byte数组转换为二进制字符串,每个字节以","隔开
     **/
    public static String conver2HexStr(byte[] b) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            result.append(Long.toString(b[i] & 0xff, 2) + ",");
        }
        return result.toString().substring(0, result.length() - 1);
    }

    /**
     * 二进制字符串转换为byte数组,每个字节以","隔开
     **/
    public static byte[] conver2HexToByte(String hex2Str) {
        String[] temp = hex2Str.split(",");
        byte[] b = new byte[temp.length];
        for (int i = 0; i < b.length; i++) {
            b[i] = Long.valueOf(temp[i], 2).byteValue();
        }
        return b;
    }


    /**
     * byte数组转换为十六进制的字符串
     **/
    @Synchronized
    public static String conver16HexStr(byte[] b) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            if ((b[i] & 0xff) < 0x10)
                result.append("0");
            result.append(Long.toString(b[i] & 0xff, 16));
        }
        return result.toString().toUpperCase();
    }

    /**
     * 十六进制的字符串转换为byte数组
     **/
    @Synchronized
    public static byte[] conver16HexToByte(String src) {
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            ret[i] = (byte) Integer
                    .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
        }
        return ret;
    }
}
